package messages 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class MessageLoading extends Sprite
	{
		/**
		 * Show message after frames
		 */
		private var _pauseBeforeShow:int;
		
		private var SHOW_TIME_MIN:int = 24;
		
		private var _timer:int;
		
		private var _title:String;
		private var _txtTitle:TextField;
		private var _windowWidth:Number = 562;
		private var _windowHeigth:Number = 266;
		
		private var _markedForHide:Boolean;
		private var _messageWindow:DisplayObject;
		
		public function MessageLoading(showAfter:int = 0) 
		{
			_pauseBeforeShow = showAfter;
		
		}
		
		
		private function handleEnterFrame(e:Event):void 
		{
			_timer--;
			if (_timer <= 0)
			{
				removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
				addEventListener(Event.ENTER_FRAME, waitForHide);
				_timer = SHOW_TIME_MIN;
				showMessage();
			}
		}
		
		private function waitForHide(e:Event):void 
		{
			if (_timer >= 0)
			{
				_timer--;
			}
			if ((_timer <= 0) && _markedForHide)
			{
				removeEventListener(Event.ENTER_FRAME, waitForHide);
				if (this.parent != null)
				{
					this.parent.removeChild(this);
					hideMessage();
				}
			}
		}
		
		public function show(showAfter:int):void
		{
			_markedForHide = false;
			_timer = showAfter;
			addEventListener(Event.ENTER_FRAME, handleEnterFrame);
		}
		
		public function hide():void
		{
			_markedForHide = true;
			
			if (_messageWindow == null || _messageWindow.parent == null)
			{
				hideNow();
			}
		}
		
		private function hideNow():void 
		{
			removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
			removeEventListener(Event.ENTER_FRAME, waitForHide);
			hideMessage();
		}
		
		
		private function hideMessage():void
		{
			if ((_messageWindow != null) && (_messageWindow.parent!=null))
			{
				_messageWindow.parent.removeChild(_messageWindow);
			}
		}
		
		private function showMessage():void 
		{
			if (_messageWindow == null)
			{
				_messageWindow = new MessageLoadingMov();
			}
			addChild(_messageWindow);
		}
		
		
		public function get windowWidth():Number 
		{
			return _windowWidth;
		}
		
		public function get windowHeigth():Number 
		{
			return _windowHeigth;
		}
		
		public function get pauseBeforeShow():int 
		{
			return _pauseBeforeShow;
		}
		
		public function set pauseBeforeShow(value:int):void 
		{
			_pauseBeforeShow = value;
		}
		
	}

}